// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 贵阳巴士快运旅游有限公司
// +----------------------------------------------------------------------
//Author:SUXU<zsx317127523@163.com>
// +----------------------------------------------------------------------

import { spread } from "@/api/user";
import Cache from "@/utils/cache";

/**
 * 静默授权绑定上下级，使用在已经登录后扫描了别人的推广二维码
 * @param {Object} puid
 */
export function silenceBindingSpread() {
	//#ifdef H5
	let puid = Cache.get('spread');
	//#endif 
	//#ifdef MP || APP-PLUS
	let puid = getApp().globalData.spread;
	//#endif

	puid = parseInt(puid);
	if (Number.isNaN(puid)) {
		puid = 0;
	}
	if (puid) {
		spread(puid).then(res => {}).catch(res => {
			//#ifdef H5
			Cache.clear("spread");
			//#endif
			
			//#ifdef MP || APP-PLUS
			getApp().globalData.spread = 0;
			getApp().globalData.code = 0;
			//#endif
		});
	} else {
		Cache.set('spread', 0);
	}
}

export function isWeixin() {
	return navigator.userAgent.toLowerCase().indexOf("micromessenger") !== -1;
}

export function parseQuery() {
	const res = {};

	const query = (location.href.split("?")[1] || "")
		.trim()
		.replace(/^(\?|#|&)/, "");

	if (!query) {
		return res;
	}

	query.split("&").forEach(param => {
		const parts = param.replace(/\+/g, " ").split("=");
		const key = decodeURIComponent(parts.shift());
		const val = parts.length > 0 ? decodeURIComponent(parts.join("=")) : null;

		if (res[key] === undefined) {
			res[key] = val;
		} else if (Array.isArray(res[key])) {
			res[key].push(val);
		} else {
			res[key] = [res[key], val];
		}
	});

	return res;
}

// #ifdef H5
const VUE_APP_WS_URL = process.env.VUE_APP_WS_URL || `ws://${location.hostname}:20001`;
export {
	VUE_APP_WS_URL
}
// #endif

export function openKf(){
	// #ifdef MP-WEIXIN
	wx.openCustomerServiceChat({
		//企业微信的企业id
		corpId: 'wwba8b8d508968a0f8',
		extInfo: {
			//客服链接
			url: 'https://work.weixin.qq.com/kfid/kfcaea708c2c0d651d2'
		},
		success: (e) => {
			console.log('e', e)
			
			// uni.showToast({
			// 	title: e,
			// })
			// return;
		},
		fail: (err) => {
			console.log('err', err)
			
			uni.showToast({
				title: err.errMsg,
				icon: 'error'
			})
			return;
		}
	})
	// #endif
	
	// #ifdef APP-PLUS
	let sweixin = null
	plus.share.getServices(res => {
		sweixin = res.find(i => i.id === 'weixin')
		if (sweixin) {
			sweixin.openCustomerServiceChat({
				corpid: 'wwba8b8d508968a0f8',
				url:'https://work.weixin.qq.com/kfid/kfcaea708c2c0d651d2'
			})
		} else {
			// plus.nativeUI.alert('当前环境不支持微信操作')
			uni.showToast({
				title: "当前环境不支持微信操作",
				icon: 'error'
			})
		}
	}, function() {
		uni.showToast({
			title: "获取服务失败，不支持该操作。" + JSON.stringify(e),
			icon: 'error'
		})
	})
	// #endif
	
	// #ifdef MP-ALIPAY
	uni.makePhoneCall({
		phoneNumber: '13765279112'
	})
	// #endif
}


export default parseQuery;
